package org.sun.sample.pojo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理
 */
class ProxySample implements IReflectProxy{

    public ProxySample() {
        LogInvocationHandler handler = new LogInvocationHandler(this);
        IReflectProxy sample = (IReflectProxy) handler.dynamicProxy();
        sample.register();
        sample.login();
        sample.logout();
    }

    public void register() {

    }

    public void login() {

    }

    public void logout() {

    }

}

class LogInvocationHandler implements InvocationHandler {

    protected Object target;

    public LogInvocationHandler(Object target) {
        this.target = target;
    }

    public Object dynamicProxy() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) {
        Object result = null;
        try {
            result = method.invoke(target, args);
        } catch (Exception exp) {

        }
        System.out.println("Invoke:" + method.getName());
        return result;
    }
}